$(function () {
    //变量抽取
    var product_dataGrid = $("#product_dataGrid");
    var product_form = $("#product_form");
    var product_dialog = $("#product_dialog");

    product_dataGrid.datagrid({
        fitColumns: true,//列自适应
        url: "/product/query",
        fit: true,
        columns: [[
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'name', title: '保险名称', width: 100},
            {field: 'company', title: '保险机构', width: 100,formatter:function (value) {
                    return value?value.name:"";
                }},
            {field: 'year', title: '保障年限', width: 100},
            {field: 'money', title: '保额', width: 100},
            {field: 'annualFee', title: '基本年费', width: 100},
            {
                field: 'saleState', title: '销售状态', width: 100, formatter: function (value, row, index) {
                    return value ? "在售" : "<font color='red'>停售</font>";
                }
            },
            {
                field: 'deductState', title: '免赔状态', width: 100, formatter: function (value, row, index) {
                    return value == 1 ? "是" : "<font color='red'>否</font>";
                }
            }
        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true//只允许选择一行
    });

    /* 新增 编辑 弹框 */
    product_dialog.dialog({
        width: 280,
        height: 340,
        buttons: "#btns",
        closed: true,
        resizable:true,
        onClose: function () {
            product_form.form("clear");
        }
    })

    var methodObj = {
        searchForm: function () {
            var keyword = $("[name=keyword]").val();
            var saleState = $("[name=saleState]").val();
            var deductState = $("[name=deductState]").val();
            var companyId = $("[name=companyId]").val();

            product_dataGrid.datagrid('load', {
                keyword: keyword,
                saleState:saleState,
                deductState:deductState,
                companyId:companyId
            });
        },
        add: function () {
            product_dialog.dialog("setTitle", "新增");
            product_dialog.dialog("open");
        },
        save: function () {
            product_form.form('submit', {
                url: "/product/saveOrUpdate",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            methodObj.cancel();
                            product_dataGrid.datagrid("load");
                        })
                    } else {
                        $.messager.alert("提示", "操作失败", "error");
                    }
                }
            })
        },
        cancel: function () {
            product_dialog.dialog("close");
        },
        edit: function () {
            var row = product_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中编辑数据', 'info');
                return;
            }
            if(row){
                row["company.id"]=row.company.id;
            }
            product_form.form("load", row);
            product_dialog.dialog("setTitle", "编辑");
            product_dialog.dialog("open");
        },
        del:function () {
            var row = product_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中删除数据', 'info');
                return;
            };

            $.get("/product/delete", {id: row.id}, function (data) {
                if (data.success) {
                    $.messager.alert('提示', '操作成功', 'info', function () {
                        product_dataGrid.datagrid("reload");
                    })
                } else {
                    $.messager.alert('提示', data.msg, 'error')
                }
            })
        },
        reload: function () {
            product_dataGrid.datagrid("load",{});
           $(".selectState").combobox('clear');
        },

    };

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });
})
